package org.android.sss;

import org.android.tools.Global;
import org.android.tools.SSSHelper;
import org.android.tools.Shop;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class SearchForProductsActivity extends Activity implements AdapterView.OnItemSelectedListener {
	
	private TextView textViewInfoShop;
	private Shop choosenShop = null;
	private Button buttonYes, buttonNo;
	
	private Spinner spinnerPossibleShops;
	private String array_spinner[] = null;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_for_products);
        
        buttonYes = (Button)findViewById(R.id.buttonYesForShops);
        buttonNo = (Button)findViewById(R.id.buttonNoForShops);
        
        textViewInfoShop = (TextView)findViewById(R.id.textViewInfoShop);
        textViewInfoShop.setText(Global.infoSearchForProduct);
        spinnerPossibleShops = (Spinner)findViewById(R.id.spinnerPossibleShops);
        spinnerPossibleShops.setOnItemSelectedListener(this);
        
        if (Global.shops.length == 0) {
        	Global.shops = SSSHelper.getShops();
        	buttonYes.setText("Choose");
        	buttonNo.setVisibility(Button.INVISIBLE);
        }
        else {
        	buttonYes.setText("Yes");
        	buttonNo.setVisibility(Button.VISIBLE);
        }
        setShops();
        
		buttonNo.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Global.shops = SSSHelper.getShops();
	        	buttonYes.setText("Choose");
	        	buttonNo.setVisibility(Button.INVISIBLE);
	        	setShops();
			}
		});
		
		buttonYes.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Global.products = SSSHelper.getProductsByShop(choosenShop);
				
				
				Intent myIntent = new Intent(v.getContext(), SearchForProductsInShopActivity.class);
                startActivityForResult(myIntent,0);
			}
		});
        
	}
	
	private void setShops() {
		array_spinner=new String[Global.shops.length];
		for (int i = 0; i < Global.shops.length; i++) {
			Integer numer = Global.shops[i].number;
			array_spinner[i] = Global.shops[i].name + ", " + Global.shops[i].street + " " + numer.toString();
		}
		ArrayAdapter adapter = new ArrayAdapter(SearchForProductsActivity.this, android.R.layout.simple_spinner_item, array_spinner);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerPossibleShops.setAdapter(adapter);
	}
	
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		choosenShop = Global.shops[arg2];
	}

	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
		
	}
}
